package fairygui.editor.handlers
{
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.gui.EUIProject;
   import fairygui.editor.utils.Logger;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.events.FileListEvent;
   import flash.filesystem.File;
   
   public class ProjectRefreshHandler
   {
       
      
      private var _project:EUIProject;
      
      public function ProjectRefreshHandler(param1:EUIProject)
      {
         super();
         this._project = param1;
      }
      
      public function run() : void
      {
         if(this._project.refreshRunnning)
         {
            return;
         }
         this._project.refreshRunnning = true;
         var _loc1_:File = new File(this._project.assetsPath);
         _loc1_.addEventListener(FileListEvent.DIRECTORY_LISTING,this.onProjectListing);
         _loc1_.getDirectoryListingAsync();
      }
      
      private function onProjectListing(param1:FileListEvent) : void
      {
         var pkg:EUIPackage = null;
         var pkgId:String = null;
         var changed:Boolean = false;
         var file:File = null;
         var pkgs:Vector.<EUIPackage> = null;
         var evt:FileListEvent = param1;
         var arr:Array = evt.files;
         for each(file in arr)
         {
            if(file.isDirectory)
            {
               pkg = this._project.getPackageByName(file.name);
               if(!pkg)
               {
                  pkgId = this.getPackageId(file);
                  if(pkgId)
                  {
                     pkg = this._project.getPackage(pkgId);
                     if(pkg)
                     {
                        if(new File(pkg.basePath).exists)
                        {
                           continue;
                        }
                        try
                        {
                           pkg.renameItem(pkg.rootItem,file.name);
                        }
                        catch(err:Error)
                        {
                           Logger.print("refresh error " + err.getStackTrace());
                        }
                     }
                     else
                     {
                        this._project.addPackage(file);
                     }
                     changed = true;
                  }
               }
            }
         }
         pkgs = this._project.getPackageList();
         for each(pkg in pkgs)
         {
            if(!new File(pkg.basePath).exists)
            {
               this._project.deletePackage(pkg.id);
               changed = true;
            }
         }
         if(changed)
         {
            this._project.editorWindow.mainPanel.libPanel.updatePackages();
         }
         pkg = this._project.editorWindow.mainPanel.getActivePackage();
         if(pkg)
         {
            pkg.refresh();
         }
         else
         {
            this._project.refreshRunnning = false;
         }
      }
      
      private function getPackageId(param1:File) : String
      {
         var _loc2_:File = param1.resolvePath("package.xml");
         if(!_loc2_.exists)
         {
            return null;
         }
         var _loc3_:String = UtilsFile.loadString(_loc2_,null,200);
         return UtilsStr.getBetweenSymbol(_loc3_,"id=\"","\"");
      }
   }
}
